<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
		</script>
	</head>
	<body>
		<input type="text" name="" id="name" value="" placeholder="ni" />

		<input type="number" name="" id="id" value="12" />
		<input type="number" name="" id="phone" value="123" />
		<input type="number" name="" id="salary" value="1234" />
		<button type="button" onclick="add()">确定</button>
		<table id="tb">

			<th>
			<td>名字</td>
			<td>ID</td>
			<td>电话</td>
			<td>工资</td>
			</th>
		</table>
	</body>
	<!-- 不要屈服 -->
	<script type="text/javascript">
		console.log(name)
		var idArray = new Array();

		//初始化变量必须放在函数中执行
		function isNull() {
			var name = $("#name").val()
			var id = $("#id").val()
			var phone = $("#phone").val()
			var salary = $("#salary").val()

			if (name == "" || id == "" || phone == "" || salary == "") {
				alert("请输入")
				return false

			}
		}
		var idArray = new Array()

		function add() {
			if (isNull() == false) {
				return
			}
			var name = $("#name").val()
			var id = $("#id").val()
			var phone = $("#phone").val()
			var salary = $("#salary").val()
			if (isDuplicate(id) == false) {
				return
			}

			// console.log(name)
			var $td1 = $("<td>" + name + "</td>")
			var $td2 = $("<td>" + id + "</td>")
			var $td3 = $("<td>" + phone + "</td>")
			var $td4 = $("<td>" + salary + "</td>")
			var $tr = $("<tr></tr>")
			$tr = $tr.append($td1)
			$tr = $tr.append($td2)
			$tr = $tr.append($td3)
			$tr = $tr.append($td4)
			$("table").append($tr)


		}
		// 这个是直接从表格中提取的，只做提示用
		/* function isDuplicate() {
			$("table").each(function() {
				var $id = $(this).find("td").eq(5).text()
				console.log($id)
				if (idArray.find(function() {
						return $id
					}) == undefined) {
					idArray.push($id)
					console.log(idArray)
					return true
				} else {
					console.log("id is duplicate")
					return false
				}
			})
		} */
		// Array对象的find和every方法对此例应该都行，但显然没有indexof好用
		function isDuplicate(id) {
			if (idArray.lastIndexOf(id) == -1) {
				idArray.push(id)
				console.log("success")
				return true
			} else {
				console.log("id is duplicate")
				return false
			}
		}
	</script>
</html>
